import axios from "axios";
import message from '@/utils/message';

// 创建一个axios实例
const request = axios.create({
    baseURL:'/api/community-nursing',
    timeout: 60000 // 设置超时时间为5s

});
// request拦截器 ==> 对请求参数进行处理
request.interceptors.request.use(
    config => {
        // 可以在发送请求之前做些事情
        // 比如请求参数的处理、在headers中携带token等等
        // if (getToken()) {
        //     config.headers['token'] = getToken()
        // }
        return config;
    }, error => {
        // 处理请求错误
        console.log(error); // for debug
        Promise.reject(error);
    }
);

// respone拦截器 ==> 对响应做处理
request.interceptors.response.use(
    response => {
        if (response.config.methodType == 'download'){
            const blob = new Blob([response.data], {type: response.data.type});
            const fileName = decodeURIComponent(response.headers['content-disposition'].substring(28));
            // 是IE >=IE10
            if (window.navigator.msSaveBlob) {
                window.navigator.msSaveBlob(blob, fileName);
            } else {
                const objectURL = window.URL.createObjectURL(blob);
                const link = document.createElement('a');
                link.href = objectURL;
                link.download = fileName;
                link.click();
                window.URL.revokeObjectURL(objectURL);
            }
            return true;
        }
        const res = response.data;
        // 如果自定义的返回码不等于200, 就返回一个错误
        if (res.return_code == 'FAIL') {
            if (res.return_msg) {
                message.error(res.return_msg, 1)
            } else {
                message.error(`接口:${response.request.responseURL}报错!`)
            }
            return false
        } else {
            return res;
        }
    }, error => {
        // 判断error的status代码，并将对应的信息告知用户
        let text = "";
        let err = JSON.parse(JSON.stringify(error));
        console.log(err);
        if (err.response) { 
            if (err.response.status) {
                switch (error.response.status) {
                    case 400:
                        text = "请求错误(400)，请重新申请";
                        break;
                    case 401:
                        let homeUrl = `${window.location.protocol}//${window.location.host}`;
                        let url = error.response.data + homeUrl;
                        let param = url.split('service=');
                        //sessionStorage.setItem(`${Date.now().toString()}:login`,error.response.data);
                        console.log(`${Date.now().toString()}:login=` + error.response.data);
                        window.location.href = param[0] + 'service=' + param[1] + "?profile=nsbd";
                        break;
                    case 403:
                        text = "拒绝访问(403)";
                        break;
                    case 404:
                        text = "请求出错(404)";
                        break;
                    case 408:
                        text = "请求超时(408)";
                        break;
                    case 500:
                        text = "服务器错误(500)！";
                        break;
                    case 501:
                        text = "服务未实现(501)";
                        break;
                    case 502:
                        text = "网络错误(502)";
                        break;
                    case 503:
                        text = "服务不可用(503)";
                        break;
                    case 504:
                        text = "网络超时(504)";
                        break;
                    case 505:
                        text = "HTTP版本不受支持(505)";
                        break;
                    default:
                        text = "网络连接出错";
                }
            } else {
                text = "连接服务器失败,请退出重试!";
            }
            message.error('接口' + error.config.url + text);
        }

        return false;
    }
);

// 将写好的axios实例暴露出去
export default request;